﻿using NetCore8Demo.Models.Communication.SerialPortFolder;
using System.IO.Ports;
using NetCore8Demo.Tool;

namespace NetCore8Demo.Views.Communication.SerialPortFolder
{
    public partial class SerialPortSetUc : UserControl
    {
        public SerialPortSetUc()
        {
            InitializeComponent();
        }

        public Guid Id;
        public bool InitFlag = false;


        public void Init(Guid id)
        {
            Id = id;
            InitControl();
            InitData();
        }

        public void ClearControlData()
        {
            SerialPortGroupBox.Text = "串口名称";
            PortNameComBoBox.Text = "";
            BaudRateComBoBox.Text = "";
            ParityComBoBox.Text = "";
            DataBitsComBoBox.Text = "";
            StopBitsComBoBox.Text = "";
            DataTypeComboBox.Text = "";
            IsWaitResultComboBox.Text = "";

            PortNameComBoBox.Items.Clear();
            BaudRateComBoBox.Items.Clear();
            ParityComBoBox.Items.Clear();
            DataBitsComBoBox.Items.Clear();
            StopBitsComBoBox.Items.Clear();
            DataTypeComboBox.Items.Clear();
            IsWaitResultComboBox.Items.Clear();

            this.SerialPortGroupBox.Enabled = false;

        }




        private void InitControl()
        {
            PortNameComBoBox.Items.Clear();
            BaudRateComBoBox.Items.Clear();
            ParityComBoBox.Items.Clear();
            DataBitsComBoBox.Items.Clear();
            StopBitsComBoBox.Items.Clear();
            DataTypeComboBox.Items.Clear();
            IsWaitResultComboBox.Items.Clear();

            foreach (var item in StaticSerialPort.GetAllPortName())
            {
                PortNameComBoBox.Items.Add(item);
            }


            foreach (var item in StaticSerialPort.BoundList)
            {
                BaudRateComBoBox.Items.Add(item);
            }

            foreach (var item in Enum.GetValues(typeof(Parity)))
            {
                ParityComBoBox.Items.Add(item);
            }

            foreach (var item in StaticSerialPort.DataBitList)
            {
                DataBitsComBoBox.Items.Add(item);
            }

            foreach (var item in Enum.GetValues(typeof(StopBits)))
            {
                StopBitsComBoBox.Items.Add(item);
            }

            foreach (var item in Enum.GetValues(typeof(SerialPortDataType)))
            {
                DataTypeComboBox.Items.Add(item);
            }

            foreach (var item in Enum.GetValues(typeof(YesOrNoEnum)))
            {
                IsWaitResultComboBox.Items.Add(item);
            }

            


        }

        public void InitData()
        {
            List<SerialPortInfo> serialPortInfoList = StaticSerialPort.LoadSerialPortInfoList();

            foreach (var item in serialPortInfoList)
            {
                if (item.Id == Id)
                {
                    this.PortNameComBoBox.Text = item.PortName;
                    this.BaudRateComBoBox.Text = item.BaudRate.ToString();
                    this.ParityComBoBox.Text = item.Parity.ToString();
                    this.DataBitsComBoBox.Text = item.DataBits.ToString();
                    this.StopBitsComBoBox.Text = item.StopBits.ToString();
                    this.DataTypeComboBox.Text = item.DataType.ToString();
                    this.IsWaitResultComboBox.Text = item.IsWaitData?"是":"否";
                    this.SerialPortGroupBox.Text = item.Name;
                    this.SerialPortGroupBox.Enabled = true;
                    break;
                }

            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            List<SerialPortInfo> serialPortInfoList = StaticSerialPort.LoadSerialPortInfoList();
            foreach (var item in serialPortInfoList)
            {
                if (item.Id == Id)
                {
                    item.PortName = this.PortNameComBoBox.Text;
                    item.BaudRate = this.BaudRateComBoBox.Text.ToInt32();

                    if (Enum.TryParse(this.ParityComBoBox.Text, out Parity parity))
                    {
                        item.Parity = parity;
                    }

                    if (Enum.TryParse(this.StopBitsComBoBox.Text, out StopBits stopBits))
                    {
                        item.StopBits = stopBits;
                    }

                    item.DataBits = this.DataBitsComBoBox.Text.ToInt32();

                    if (Enum.TryParse(this.DataTypeComboBox.Text, out SerialPortDataType dataType))
                    {
                        item.DataType = dataType;
                    }

                    item.IsWaitData = YesOrNoEnum.是.ToString().Equals(IsWaitResultComboBox.Text);
                    break;
                }

            }

            StaticSerialPort.SavaSerialPortInfoList(serialPortInfoList);

        }
    }
}
